package com.zlsy.learning.leetcode.linkedlist.l876;

import com.zlsy.learning.leetcode.linkedlist.l234.ListNode;

/**
 * @author zhouliang
 * @date 2020/8/5 16:43
 */
public class MiddleNode {
  /** 快慢指针 */
  public static ListNode middleNode(ListNode head) {
    ListNode slow = head;
    ListNode fast = head;
    while (fast != null && fast.next != null) {
      slow = slow.next;
      fast = fast.next.next;
    }
    return slow;
  }

  public static void main(String[] args) {
    ListNode node = new ListNode(1);
    node.add(2);
    node.add(3);
    node.add(4);
    node.add(5);
    ListNode listNode = middleNode(node);
    listNode.print();
  }
}
